<?php

namespace app\admin\model;

use think\Model;
use think\Db;

class FlashSale extends Model
{
    // 表名
    protected $name = 'flash_sale';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'start_time_text',
        'end_time_text',
        'is_end_text',
        'recommend_text',
        'status_text'
    ];

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $goods_name = Db::name('goods')->where(['id' => $row->goods_id])->value('name');
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk], 'goods_name' => $goods_name]);

            $goodsM = model('goods');
            $goodsM->saveProm($row->goods_id,'3',$row->id);
        });

        self::afterUpdate(function($row){
            
            $goods_name = Db::name('goods')->where(['id' => $row->goods_id])->value('name');
            Db::name('team_activity')->where('id', $row->id)->update(['goods_name' => $goods_name]);

            $goodsM = model('goods');
            $goodsM->saveProm($row->goods_id,'3',$row->id);
        });

        self::afterDelete(function($row){
            $goodsM = model('goods');
            $goodsM->delProm($row->goods_id);
        });
    }
    

    


    public function getRecommendList()
    {
        return ['0' => __('Recommend 0'),'1' => __('Recommend 1')];
    }     

    public function getStatusList()
    {
        return ['0' => __('Status 0'),'1' => __('Status 1'),'2' => __('Status 2')];
    }


    public function getIsEndList()
    {
        return ['0' => __('Is_end 0'),'1' => __('Is_end 1')];
    }

    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['start_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['end_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getIsEndTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['is_end'];
        $list = $this->getIsEndList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRecommendTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['recommend'];
        $list = $this->getRecommendList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    

    protected function setStartTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setEndTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


}
